<template>
  <el-dialog :title="`${title}商品规格`" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="ruleForm">
      <el-form-item label="规格名称：" label-width="100px" prop="specification_name">
        <el-input style="width: 50%" v-model="form.specification_name" clearable></el-input>
      </el-form-item>
      <el-form-item
        v-for="(item, idx) in form.specification_value"
        :key="idx"
        :label="`规格值${idx + 1}：`"
        label-width="100px"
        :prop="`specification_value.${idx}`"
        :rules="{
          required: true,
          message: '规格值不能为空',
          trigger: 'blur',
        }"
      >
        <div style="display: flex">
          <el-input style="width: 60%" v-model="form.specification_value[idx]" clearable></el-input>
          <div style="padding-left: 10px">
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(idx)"
              v-if="form.specification_value.length > 1"
            ></el-button>
            <el-button
              :type="form.specification_value[idx] ? 'primary' : 'info'"
              icon="el-icon-plus"
              @click="handleAdd"
              :disabled="form.specification_value[idx] == ''"
              v-if="idx === form.specification_value.length - 1"
            ></el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="confirm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { add_goods_specification, edit_goods_specification } from '@/api/goods'
  export default {
    emits: ['getList'],
    data() {
      return {
        title: '添加',
        dialogVisible: false,
        form: {
          specification_value: [''],
        },
        value: [''],
        rules: {
          specification_name: [{ required: true, message: '请输入规格名称', trigger: 'blur' }],
          specification_value: [{ required: true, message: '请输入规格值', trigger: 'blur' }],
        },
      }
    },
    watch: {
      value(n) {
        console.log('n,o: ', n)
        if (n.length == 1 && n[0] == '') {
          return (this.form.specification_value = [])
        }
        this.form.specification_value = n
      },
    },
    methods: {
      confirm(formName) {
        console.log(this.form)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm(`是否确定${this.title}？`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            })
              .then(async () => {
                let API = this.type == '添加' ? add_goods_specification(this.form) : edit_goods_specification(this.form)
                await API.then((data) => {
                  console.log('data: ', data)
                  if (data.code == 200) {
                    this.$message({
                      type: 'success',
                      message: ` ${this.title}成功`,
                    })
                    this.handleClose()
                    this.$emit('getList')
                  } else {
                    this.$message({
                      type: 'error',
                      message: data.data,
                    })
                  }
                })
              })
              .catch(() => {})
          } else {
            return false
          }
        })
      },
      open(type, row) {
        console.log('row: ', row)
        this.title = type === '1' ? '添加' : '修改'
        if (type === '2') {
          this.form = row
          this.form.specification_value = this.form.specification_value.map((item) => {
            if (item.includes('|')) {
              return item.replace(/\|/g, '￥')
            }
            return item
          })
        }
        this.dialogVisible = true
      },
      handleDelete(idx) {
        this.form.specification_value.splice(idx, 1)
      },
      handleAdd() {
        this.form.specification_value.push('')
      },
      handleClose() {
        this.form = {
          specification_value: [''],
        }
        this.dialogVisible = false
      },
    },
  }
</script>

<style></style>
